﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace PickGold.Nulls
{
	/// <summary/>
	[Serializable]
	[ComVisible(true)]
	public sealed class NullAsyncResult : MarshalByRefObject, IAsyncResult
	{
		/// <summary/>
		public NullAsyncResult(object asyncState)
		{
			this.AsyncState = asyncState;
			this.AsyncWaitHandle = NullWaitHandle.Value;
			this.CompletedSynchronously = true;
			this.IsCompleted = true;
		}

		/// <summary/>
		public object AsyncState { set; get; }

		/// <summary/>
		public WaitHandle AsyncWaitHandle { set; get; }

		/// <summary/>
		public bool CompletedSynchronously { set; get; }

		/// <summary/>
		public bool IsCompleted { set; get; }
	}

	/// <summary/>
	[Serializable]
	[ComVisible(true)]
	public sealed class NullWaitHandle : WaitHandle
	{
		private NullWaitHandle()
		{
			if (NullWaitHandle.Value != null)
				throw new NotSupportedException("Use Value.");
		}

		/// <summary/>
		public override void Close() { }

		/// <summary>不等待，永远返回为 True</summary>
		public override bool WaitOne()
		{
			return true;
		}

		/// <summary>不等待，永远返回为 True</summary>
		public override bool WaitOne(int millisecondsTimeout)
		{
			return true;
		}

		/// <summary>不等待，永远返回为 True</summary>
		public override bool WaitOne(int millisecondsTimeout, bool exitContext)
		{
			return true;
		}

		/// <summary>不等待，永远返回为 True</summary>
		public override bool WaitOne(TimeSpan timeout)
		{
			return true;
		}

		/// <summary>不等待，永远返回为 True</summary>
		public override bool WaitOne(TimeSpan timeout, bool exitContext)
		{
			return true;
		}

		/// <summary/>
		protected override void Dispose(bool explicitDisposing)
		{
		}

		/// <summary/>
		public readonly static WaitHandle Value = new NullWaitHandle();
	}
}
